<template>
    <div class="search-result">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
            error-text="请求失败，点击重新加载">
            <van-cell v-for="(item, index) in list" :key="index" :title="item.title" />
        </van-list>
    </div>
</template>

<script>
import { getSearchResult } from '../../../api/search'

export default {
    name: 'SearchResult',
    props: {
        searchText: {
            type: String,
            required: true
        }
    },
    data() {
        return {
            list: [],
            loading: false,
            finished: false,
            page: 1,
            perPage: 20,
            error: false,
        };
    },
    methods: {
        async onLoad() {
            try {
                // if (Math.random() > 0.2) {
                //     JSON.parse('abc')
                // }
                const { data } = await getSearchResult({
                    page: this.page,
                    per_page: this.perPage,
                    q: this.searchText
                })
                // console.log(data);
                const { results } = data.data
                this.list.push(...results)
                if (results.length) {
                    this.page++
                } else {
                    this.finished = true
                }
            } catch (err) {
                this.$toast('数据获取失败')
                this.error = true
            }
            this.loading = false
        },
    },
}
</script>

<style>
</style>